package com.gun3y.entity.result;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

import org.apache.commons.lang3.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.gun3y.entity.media.PropertyType;
import com.gun3y.entity.pattern.FieldType;
import com.gun3y.utils.Commons;

@XmlRootElement(name = "field")
@XmlAccessorType(XmlAccessType.NONE)
public class ResultField {

    @XmlAttribute
    private PropertyType name;

    @XmlAttribute
    private FieldType fieldType;

    @XmlValue
    private String value;

    public ResultField(PropertyType fieldName, String value, FieldType fieldType) {
	this.name = fieldName;
	this.value = StringUtils.isNotBlank(value) ? Commons.cleanText(value) : value;
	this.fieldType = fieldType;
    }

    public boolean validate() {
	return this.name != null && StringUtils.isNotBlank(this.value);
    }

    public PropertyType getName() {
	return name;
    }

    public void setName(PropertyType name) {
	this.name = name;
    }

    public String getValue() {
	return value;
    }

    public void setValue(String value) {
	if (value != null) {
	    value = value.trim();
	}
	this.value = value;
    }

    public FieldType getFieldType() {
	return fieldType;
    }

    public void setFieldType(FieldType fieldType) {
	this.fieldType = fieldType;
    }

    @Override
    public String toString() {
	Gson gson = (new GsonBuilder()).setPrettyPrinting().create();
	return gson.toJson(this, this.getClass());
    }
}
